package com.acq.sdk;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;


public class JsonUtil {

	private static final Logger log = Logger.getLogger(JsonUtil.class);
	private static ObjectMapper mapper = new ObjectMapper();

	/**
	 * Object to JSON
	 * 
	 * @param obj
	 * @return String
	 */
	public static <T> String toJson(T obj) {
		try {
			return mapper.writeValueAsString(obj);
		} catch (Exception e) {
			log.error("write to json string error:" + obj, e);
			return null;
		}
	}

	/**
	 * JSON to Object
	 * 
	 * @param jsonString
	 * @param clazz
	 * @return <T> T
	 */
	public static <T> T fromJson(String jsonString, Class<T> clazz) {
		try {
			return mapper.readValue(jsonString, clazz);
		} catch (Exception e) {
			log.error("parse json string error:" + jsonString, e);
			return null;
		}
	}
	

}
